package com.example.mediaservice;


import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MediaItem {
    final String id;
    final String title;
    final String artist;
    final long duration;

    public MediaItem(String id, String title, String artist, long duration) {
        this.id = id;
        this.title = title;
        this.artist = artist;
        this.duration = duration;
    }

    // 扫描本地音乐的示例方法
    public static List<MediaItem> scanLocalMusic(Context context) {
        List<MediaItem> mediaItems = new ArrayList<>();

        // 适配不同 Android 版本的权限检查
        boolean hasPermission = false;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            // Android 13 及以上检查 READ_MEDIA_AUDIO 权限
            hasPermission = context.checkSelfPermission(Manifest.permission.READ_MEDIA_AUDIO)
                    == PackageManager.PERMISSION_GRANTED;
        } else {
            // Android 12 及以下检查 READ_EXTERNAL_STORAGE 权限
            hasPermission = context.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_GRANTED;
        }

        if (!hasPermission) {
            Log.w("MediaItem", "No permission to read media files. SDK: " + Build.VERSION.SDK_INT);
            Toast.makeText(context, "无权限访问媒体文件", Toast.LENGTH_SHORT).show();
            return mediaItems; // 返回空列表
        } else {
            Log.d("MediaItem", "Has permission to read media files");
            Toast.makeText(context, "有权限访问媒体文件", Toast.LENGTH_SHORT).show();
        }

//        String downloadPath = Environment.getExternalStorageDirectory().getPath() + "/Download/music/";

//        Toast.makeText(context, downloadPath, Toast.LENGTH_SHORT).show();
//        // 添加查询条件，只扫描Download文件夹中的音乐文件
//        String selection = MediaStore.Audio.Media.DATA + " LIKE ?";
//        String[] selectionArgs = {downloadPath + "%"};
        // 定义需要查询的列
        String[] projection = {
                MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.DURATION,
                MediaStore.Audio.Media.DATA
        };

        // 查询音乐文件
        Cursor cursor = context.getContentResolver()
                .query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                        projection,
                        null,
                        null,
                        null);
        Toast.makeText(context, cursor.getCount() + " 个音乐文件", Toast.LENGTH_SHORT).show();
        // 遍历结果并创建 MediaItem 对象
        if (cursor != null && cursor.moveToFirst()) {
            do {
                String id = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
                String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
                String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
                long duration = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
                mediaItems.add(new MediaItem(id, title, artist, duration));
            } while (cursor.moveToNext());
            cursor.close();
        }
        Log.d("MediaItem", "Found " + mediaItems.size() + " music files");
        return mediaItems;
    }
}
